package com.mind.adjust.service.impl;

import com.mind.adjust.domain.vo.dashboard.DashboardVo;
import com.mind.adjust.domain.vo.dashboard.RuleOverview;
import com.mind.adjust.domain.vo.dashboard.RulePassVo;
import com.mind.adjust.domain.vo.dashboard.TicketCountVo;
import com.mind.adjust.mapper.AjMonthReportMapper;
import com.mind.adjust.mapper.DashboardMapper;
import com.mind.adjust.mapper.TicketTaskCountMapper;
import com.mind.common.utils.DateUtils;
import com.mind.common.utils.PageUtils;
import com.mind.system.domain.AjMonthReport;
import com.mind.system.domain.AjTicketTaskCount;
import org.springframework.stereotype.Component;

import jakarta.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.*;

@Component
public class DashboardService {
    @Resource
    private AjMonthReportMapper monthReportMapper;
    @Resource
    private DashboardMapper dashboardMapper;

    @Resource
    private TicketTaskCountMapper ticketTaskCountMapper;
    public Integer getTicketCount(DashboardVo dashboardVo) {
        Integer ticketCount = dashboardMapper.getTicketCount(dashboardVo);
        Integer aLong = ticketTaskCountMapper.selectCountByDate(dashboardVo.getStartTime(), dashboardVo.getEndTime());
        aLong = aLong == null ? 0 : aLong;
        return ticketCount - aLong;
    }

    public String getTicketPassRate(DashboardVo dashboardVo) {

        Integer passTicketCount = dashboardMapper.getPassTicketCount(dashboardVo);
        Integer ticketCount = dashboardMapper.getTicketCount(dashboardVo);
        Integer aLong = ticketTaskCountMapper.selectCountByDate(dashboardVo.getStartTime(), dashboardVo.getEndTime());
        aLong = aLong == null ? 0 : aLong;
        ticketCount = ticketCount - aLong;
        if(ticketCount == 0 ) {
            return "0%";
        }
        return String.format("%.2f", passTicketCount * 1.0 / ticketCount * 100) + "%";
    }

    public RulePassVo getRulePassRate(DashboardVo dashboardVo) {
        RulePassVo rulePassRate = dashboardMapper.getRulePassRate(dashboardVo);
        if(rulePassRate == null) {
            rulePassRate = new RulePassVo(){{
                setPassCount(0);
                setTotalCount(0);
            }};
        }
        if(rulePassRate.getTotalCount() == 0) {
            rulePassRate = new RulePassVo();
            rulePassRate.setPassRate("0%");
        }else{
            rulePassRate.setPassRate(String.format("%.2f", rulePassRate.getPassCount() * 1.0 / rulePassRate.getTotalCount()  * 100) + "%");
        }
        return rulePassRate;
    }

    public List<TicketCountVo> getTicketOverview(DashboardVo dashboardVo) {
        long month = DateUtils.getFullMonthsBetween(dashboardVo.getStartTime(), dashboardVo.getEndTime());
        List<LocalDate[]> days;
        // 如果时间跨度小于3个月，按周统计
        if(month <= 3){
            days = DateUtils.getFirstAndLastDaysOfWeek(dashboardVo.getStartTime(), dashboardVo.getEndTime());
        }else{
            days = DateUtils.getFirstAndLastDaysOfMonth(dashboardVo.getStartTime(), dashboardVo.getEndTime());
        }
        List<TicketCountVo> ticketCountVos = new ArrayList<>();
        for(LocalDate[] day : days) {
            TicketCountVo ticketCountVo = new TicketCountVo();
            ticketCountVo.setDay(day[0].toString());
            dashboardVo = new DashboardVo();
            LocalDateTime start = day[0].atStartOfDay();
            LocalDateTime end = day[1].atStartOfDay().plusDays(1).plusSeconds(-1);
            dashboardVo.setStartTime(start.toString());
            dashboardVo.setEndTime(end.toString());
            Integer ticketCount = dashboardMapper.getTicketCount(dashboardVo);
            String startTime = LocalDate.parse(day[0].toString()).toString();
            String endTime = LocalDate.parse(day[1].toString()).toString();
            Integer aLong = ticketTaskCountMapper.selectCountByDate(startTime, endTime);
            aLong = aLong == null ? 0 : aLong;
            ticketCountVo.setTotalCount(ticketCount-aLong);
            ticketCountVo.setPassCount(dashboardMapper.getPassTicketCount(dashboardVo));
            if(ticketCountVo.getTotalCount() == 0) {
                ticketCountVo.setPassRate("0%");
            }else {
                ticketCountVo.setPassRate(String.format("%.2f", ticketCountVo.getPassCount() * 1.0 / ticketCountVo.getTotalCount() * 100) + "%");
            }
            ticketCountVos.add(ticketCountVo);
        }
        return ticketCountVos;
    }


    public Map<String, Map<String, Integer>> getRuleOverview(DashboardVo dashboardVo) {
        Map<String, Map<String, Integer>> result = new HashMap<>();
        List<RuleOverview> ruleOverviews = dashboardMapper.selectRuleOverview();
        result.put("原料", new HashMap<>());
        result.put("费报", new HashMap<>());
        for (RuleOverview ruleOverview : ruleOverviews) {
            if (ruleOverview.getTicketType() == null) {
                continue;
            }
            int size = new HashSet<>(Arrays.asList(ruleOverview.getTicketType().split(","))).size();
            if (ruleOverview.getTicketType().contains("YCLJSD")) {
                if (result.get("原料").containsKey(ruleOverview.getRuleset())) {
                    result.get("原料").put(ruleOverview.getRuleset(), result.get("原料").get(ruleOverview.getRuleset()) + size);
                } else {
                    result.get("原料").put(ruleOverview.getRuleset(), size);
                }
            } else {
                if (result.get("费报").containsKey(ruleOverview.getRuleset())) {
                    result.get("费报").put(ruleOverview.getRuleset(), result.get("费报").get(ruleOverview.getRuleset()) + size);
                } else {
                    result.get("费报").put(ruleOverview.getRuleset(), size);
                }
            }
        }
        return result;
    }

    public List<AjMonthReport> getRulePassRateBate(AjMonthReport report) {
        PageUtils.startPage();
        return monthReportMapper.selectMonthReportList(report);
    }
}
